package net.huashitong.ssydt.provider.common.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.huashitong.ssydt.provider.common.service.CommonService;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.11.20
 */
@Api(value = "/api/common/share", tags = {"公共模块（分享页面）"})
@Controller
@RequestMapping("/api/common/share")
public class ApiWebPageCommonShareController {

    private HttpServletRequest request;

    private CommonService commonService;

    public ApiWebPageCommonShareController(HttpServletRequest request,
                                           CommonService commonService) {
        this.request = request;
        this.commonService = commonService;
    }

    @ApiOperation(value = "分享统一跳转接口", notes = "分享统一跳转接口", produces = MediaType.TEXT_HTML_VALUE)
    @GetMapping("/{uuid}")
    public String shareForward(@PathVariable("uuid") String uuid) {
        String view = commonService.shareForward(uuid, request);
        return String.format("forward:/%s/page", getView(view));
    }

    /**
     * 文章分享页面
     */
    @GetMapping("/article/page")
    public String getShareArticlePage(Model model) {
        model = commonService.getShareArticlePage(request, model);
        return getView("article");
    }

    /**
     * 题目分享页面
     */
    @GetMapping("/question/page")
    public String getShareQuestionPage(Model model) {
        model = commonService.getShareQuestionPage(request, model);
        return getView("question");
    }

    /**
     * 测评报告分享页面
     */
    @GetMapping("/exam-report/page")
    public String getShareExamReportPage(Model model) {
        model = commonService.getShareExamReportPage(request, model);
        return getView("exam-report");
    }

    /**
     * 错误页面
     */
    @GetMapping("/no-page/page")
    public String noPage() {
        return getView("no-page");
    }

    /**
     * 返回页面
     *
     * @param view
     * @return
     */
    private String getView(String view) {
        return String.format("api/common/share/%s", view);
    }
}
